#include "LinkTree.h"
#include <string.h>
#include <MyString.h>
#include <stdlib.h>

struct Student
{
    int age;
    MyStr name;
};
typedef struct Student Stu;

Stu* CreateStu(const char *name, int age)
{
    Stu *s = (Stu *)malloc(sizeof(Stu));
    if (s == NULL)
    {
        return NULL;
    }
    s->age = age;
    InitString(&s->name);
    s->name.InsertString(&s->name, name);
    return s;
}

void FreeStudent(void *s)
{
    Stu *student = (Stu *)s;
    student->name.ClearString(&student->name);
    free(student);
}

bool isEqual(ElementType v1, ElementType v2)
{
    if (strcmp(v1, v2) == 0)
    {
        return true;
    } 
    return false;
}

void PrintTree(void *student)
{
    Stu *s = (Stu *)student;
    if (s->age == -1)
    {
        printf("%s\n", s->name.str);
    }
    else
    {
        printf("学生姓名：%s, 学生年龄：%d\n", s->name.str, s->age);
    }
}

int main()
{
    LTree tree;
    InitTree(&tree);

    LTNode *c1 = CreateTreeNode(CreateStu("251班", -1));
    LTNode *c2 = CreateTreeNode(CreateStu("253班", -1));

    ConnectBranch(tree.root, c1);
    ConnectBranch(tree.root, c2);

    LTNode *n1 = CreateTreeNode(CreateStu("张三", 20));
    LTNode *n2 = CreateTreeNode(CreateStu("李四", 21));
    LTNode *n3 = CreateTreeNode(CreateStu("王五", 22));
    LTNode *n4 = CreateTreeNode(CreateStu("张三1", 20));
    LTNode *n5 = CreateTreeNode(CreateStu("李四2", 21));
    LTNode *n6 = CreateTreeNode(CreateStu("王五3", 22));

    ConnectBranch(c1, n1);
    ConnectBranch(c1, n2);
    ConnectBranch(c1, n4);
    ConnectBranch(c1, n5);
    ConnectBranch(c1, n6);
    ConnectBranch(c2, n3);
    
    TravelTree(&tree, PrintTree);
    DisconnectBranch(c1, n1);

    // DisconnectBranch(c1, n1);
    // TravelTree(&tree);
    // printf("===============\n");
    // ConnectBranch(c2, n1);
    // TravelTree(&tree);

    // printf("树的高度：%d\n", GetTreeHeight(&tree));

    // LTNode *node = FindTreeNode(&tree, "张三", isEqual);
    // if (node != NULL)
    // {
    //     printf("找到了：%s\n", node->data);
    // }

    FreeTree(&tree, FreeStudent);
    return 0;
}